<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加博客或修改</title>
</head>
<style>
    table,
    td,
    th {
        border-style: solid;
        border-width: 1px;
    }
    #yy {
        color:blue;
    }
</style>

<body>
    <div id="yy">
        <h2>
            添加博客或修改
        </h2>
    </div>
    <table>
        <tr>
            <td>标题：</td>
            <td><input type="text" name="Title" placeholder="请输入标题"></td>
        </tr>
        <tr>
            <td>摘要：</td>
            <td><input type="text" name="Abstract" placeholder="请输入摘要"></td>
        </tr>
        <tr>
            <td>内容：</td>
            <td><input type="text" name="Content" placeholder="请输入内容"></td>
        </tr>
        <tr>
            <td>分类：</td>
            <td><input type="text" name="Classify" placeholder="请输入分类"></td>
        </tr>
        <tr>
            <td>作者：</td>
            <td><input type="text" name="Author" placeholder="请输入作者"></td>
        </tr>
        <tr>
            <td>发表时间：</td>
            <td><input type="text" name="ReleaseTime" placeholder="请输入发表时间"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="Save()"></td>
            <td><input type="button" value="取消并返回" onclick="Quxiao()"></td>
        </tr>
    </table>

    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script src="./js/init.js"></script>

    <script>
        $(function () {
            let id = getUrlParams('id');
            id = id || 0;
            getProductById(id).then(res => {
                let obj = res.data;
                $('[name=Title]').val(obj.Title);
                $('[name=Abstract]').val(obj.Abstract);
                $('[name=Content]').val(obj.Content);
                $('[name=Classify]').val(obj.Classify);
                $('[name=Author]').val(obj.Author);
                $('[name=ReleaseTime]').val(obj.ReleaseTime);
            });
        });

        function getUrlParams(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }

        function Save() {//保存
            let id = getUrlParams('id');
            let obj = {
                Title: $('[name=Title]').val(),
                Abstract: $('[name=Abstract]').val(),
                Content: $('[name=Content]').val(),
                Classify: $('[name=Classify]').val(),
                Author: $('[name=Author]').val(),
                ReleaseTime: $('[name=ReleaseTime]').val()
            };
            if (id) {
                updateProduct(id, obj).then(res => {//编辑保存
                    alert("编辑成功！");
                    location.href = "./index.html";
                });
            } else {
                let params = {
                    Title: obj.Title,
                    Abstract: obj.Abstract,
                    Content: obj.Content,
                    Classify: obj.Classify,
                    Author: obj.Author,
                    ReleaseTime: obj.ReleaseTime
                };
                addProduct(params).then(res => {//新增保存
                    alert("保存成功！");
                    location.href = "./index.html";
                });
            }
        }

        function Quxiao() {//取消并返回
            location.href = "./index.html";
        }
    </script>
</body>

</html>